<?php

namespace App\Http\Controllers;

use App\CmeData;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class TrendController extends Controller
{
    /**
     * 数据集合
     *
     * @var Collection
     */
    private $data;

    /**
     * 列名
     *
     * @var array
     */
    private $columnNames = [];

    /**
     * 最终呈现的数据格式
     *
     * @var array
     */
    private $series = [];

    /**
     * 日期集合
     *
     * @var array
     */
    private $date = [];

    /**
     * 显示的最小值
     *
     * @var int
     */
    private $min = 0;

    /**
     * 显示的最大值
     *
     * @var int
     */
    private $max = 0;


    /**
     * 根据index页面type类型来生成相应的数据集合
     *
     * @param Request $request
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
     */
    public function index(Request $request)
    {
        switch ($request->type) {
            case 'plan1':
                $this->init(CmeData::all()->sortByDesc('id')->values(), ['noncommercial_long', 'noncommercial_short']);
                $this->setSeries();
                break;
            case 'plan2':
                // ...
                break;
            default:
                $this->init(CmeData::all(), ['noncommercial_long', 'noncommercial_short']);
                $this->setSeries();
                break;
        }


        return view('trends.index', [
            'data'        => $this->data,
            'date'        => $this->date,
            'columnNames' => $this->columnNames,
            'series'      => $this->series,
            'min'         => $this->min,
            'max'         => $this->max,
        ]);
    }


    /**
     * 此处初始化数据集合/日期数组/列名
     * 注意为了所有数据的对应关系正确,只能取数据集合后在根据其生成日期数组及后续的数据,且不能分别进行排序
     *
     * @param Collection $data
     * @param array $columnNames
     * @return void
     */
    private function init($data, $columnNames)
    {
        $this->data = $data;
        $this->date = $this->data->map(function ($item) {
            return $item->date;
        })->toArray();
        $this->columnNames = $columnNames;
    }

    /**
     * 此处根据数据及要取的列名生成前端js格式,并且计算最终图表显示的最大最小值
     * 注意为了所有数据的对应关系正确,只能取数据集合后在根据其生成日期数组及后续的数据,且不能分别进行排序
     *
     * @return void
     */
    private function setSeries()
    {
        $series = [];
        foreach ($this->columnNames as $key => $columnName) {
            $data = $this->data->pluck($columnName);
            $series[] = [
                'name'      => $columnName,
                'type'      => 'line',
                'lineStyle' => [
                    'normal' => [
                        'width' => 2,
                    ],
                ],
                'data'      => $data,
            ];


            if ($key == 0) {
                $this->min = $data->min();
                $this->max = $data->max();
            } else {
                $this->min = $this->min < $data->min() ? $this->min : $data->min();
                $this->max = $this->max > $data->max() ? $this->max : $data->max();
            }
        }

        $this->series = $series;
    }
}

